package com.loveshare.imgshow.dao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.loveshare.imgshow.entity.Doubanmz;
import com.loveshare.imgshow.entity.Music;
import com.loveshare.imgshow.entity.VideoListInfo;

/**
 * 
 * ClassName: ImgShowDaoImpl 
 * @Description: 图片展示接口实现类
 * @author 爱分享
 * @date 2017年12月20日
 */
@Repository
public class ImgShowDaoImpl implements ImgShowDao{

	@Autowired
	JdbcTemplate jdbcTemplate;

	public boolean addArticleInfo(String ip,String ipregion) {
		String sql="INSERT INTO Uservisitlog (ip,ipregion,addTime) VALUES (?,?,?)";
		jdbcTemplate.update(sql,ip,ipregion,new Timestamp(System.currentTimeMillis()));
		return false;
	}

	@SuppressWarnings("rawtypes")
	public List<Doubanmz> getDoubanmzs(String tableName,int pictureType, int page, int pageSize) {
		List<Doubanmz> list=new ArrayList<Doubanmz>();
		String sql="SELECT pictureUrl,title from "+tableName;
		Object[] param={};
		if(pictureType!=-1){
			sql= sql + " WHERE pictureType=? LIMIT ?,?";
			param=new Object[]{pictureType,(page-1)*pageSize,pageSize};
		}else{
			sql= sql + " LIMIT ?,?";
			param=new Object[]{(page-1)*pageSize,pageSize};
		}
		List<Map<String,Object>> douBans=jdbcTemplate.queryForList(sql,param);  
		Doubanmz doubanmz=null;
		for (Map map : douBans) {
			doubanmz=new Doubanmz();
			//doubanmz.setMzId(map.get("mzId").toString());
			//doubanmz.setPictureType(Integer.valueOf(map.get("pictureType").toString()));
			doubanmz.setPictureUrl(map.get("pictureUrl").toString());
			doubanmz.setTitle(map.get("title").toString());
			list.add(doubanmz);
		}
		return list;
	}

	public int addDoubanmz(com.loveshare.imgshow.entity.Doubanmz doubanmz) {
		String sql="INSERT INTO DoubanMza_test(mzId,pictureUrl,title,pictureType,addTime)VALUES (?,?,?,?,now())";
		return jdbcTemplate.update(sql, UUID.randomUUID().toString(),doubanmz.getPictureUrl(),doubanmz.getTitle(),doubanmz.getPictureType());
	}

	@SuppressWarnings("rawtypes")
	public List<VideoListInfo> getVideoListInfo(int page, int pageSize) {
		List<VideoListInfo> list=new ArrayList<VideoListInfo>();
		String sql="SELECT id,videoId,title,videoSrc FROM videoList ORDER BY addTime desc LIMIT ?,?";
		Object[] param={};
		param=new Object[]{(page-1)*pageSize,pageSize};
		List<Map<String,Object>> douBans=jdbcTemplate.queryForList(sql,param);  
		VideoListInfo video=null;
		for (Map map : douBans) {
			video=new VideoListInfo();
			video.setId(Integer.valueOf(map.get("Id").toString()));
			video.setVideoId(map.get("videoId").toString());
			video.setTitle(map.get("title").toString());
			video.setVideoSrc(map.get("videoSrc").toString());
			list.add(video);
		}
		return list;
	}

	public void addMusicSearchLog(String keyword) {
		String sql = "INSERT INTO musicSearchLog (keyword,searchCount,addTime,updateTime) VALUES (?,1,NOW(),NOW())";
		jdbcTemplate.update(sql, keyword);
	}

	public void updateMusicSearchLog(String keyword) {
		String sql = "UPDATE musicSearchLog SET searchCount = searchCount+1, updateTime = NOW() WHERE keyword=?";
		jdbcTemplate.update(sql, keyword);
	}

	public int selectMusicSearchLogCount(String keyword) {
		String sql = "SELECT count(1) as count from musicSearchLog WHERE keyword=? ";
		return jdbcTemplate.queryForObject(sql, new Object[] {keyword}, Integer.class);
	}

	@SuppressWarnings("rawtypes")
	public List<Music> getMusics(String keyword, int page, int pageSize) {
		String sql = "SELECT musicId,author,pic,type,title,lrc,songid,qnUrl FROM music "
				+ " WHERE "
				+ " (author LIKE ? or title LIKE ?) LIMIT ?,? ";
		Object [] param = null;
		if(keyword != null && keyword.length() > 0){
			param =new Object[] {"%"+keyword+"%", "%"+keyword+"%", (page-1)*pageSize, pageSize};
		}else{
			param =new Object[] {(page-1)*pageSize, pageSize};
		}
		List<Music> musicList = new ArrayList<Music>();
		List<Map<String,Object>> musics=jdbcTemplate.queryForList(sql,param);
		Music music = null;
		for (Map map : musics) {
			music = new Music();
			music.setAuthor(map.get("author").toString());
			music.setTitle(map.get("title").toString());
			music.setMusicId(map.get("musicId").toString());
			music.setQnUrl(map.get("qnUrl").toString());
			musicList.add(music);
		}
		return musicList;
	}

}
